import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/config/contants.dart';
import 'package:village_android/model/health_submit_model.dart';
import 'package:village_android/pages/health/health_submit_detail_page.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/http_utlis.dart';

class HealthSubmitListPage extends StatefulWidget {
//  var userId;
  var status = "0";
  List items = [];

  HealthSubmitListPage({Key key, this.status, this.items}) : super(key: key);

  _HealthSubmitListPageState pageState = new _HealthSubmitListPageState();

  @override
  _HealthSubmitListPageState createState() => new _HealthSubmitListPageState();

  void getData() {
    pageState.getData();
  }
}
//NewsCell

class _HealthSubmitListPageState extends State<HealthSubmitListPage> {
  @override
  void initState() {
    super.initState();
    widget.items = [];
    getData();
  }

  void getData() {
    Map<String, Object> params = new Map();
    if (widget.status != "0") {
      params["status"] = widget.status;
    }
    HttpUtils.get("/api/healthPaySubmit/list", data: params, success: (data) {
      setState(() {
        widget.items = data;
      });
    }, error: (errorMsg) {
      CommonUtils.showToast(context, errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        body: widget.items.isEmpty
            ? Center(child: Text('无数据'))
            : Center(
          child: EasyRefresh(
            header: BezierHourGlassHeader(backgroundColor: Colors.red),
            footer: BezierBounceFooter(backgroundColor: Colors.red),
            onRefresh: () async {
              getData();
            },
            onLoad: () async {},
            child: ListView.separated(
                itemBuilder: (context, index) {
                  HealthSubmitModel healthSubmitModel =
                  HealthSubmitModel.fromJson(widget.items[index]);
                  return _newsCell(context, healthSubmitModel);
                },
                separatorBuilder: (context, index) =>
                    Divider(height: 1.0, color: Colors.black12),
                itemCount: widget.items.length),
          ),
        ),

      ),
    );
  }

  Future onItemClick(item) async {
    var page = new HealthSubmitDetailPage(
      healthPaySubmitId: item.healthPaySubmitId,
    );
    final result = await Navigator.push(
        context, new MaterialPageRoute(builder: (context) => page));
    if (result != null) {
      getData();
    }
  }

  //独立Item
  Widget _newsCell(BuildContext context, HealthSubmitModel item) {


    var titleBgColor = Colors.red;
    if(item.status == K_SUBMIT_NORMAL) {
      titleBgColor = Colors.blueGrey;
    }else if(item.status == K_SUBMIT_ING) {
      titleBgColor = Colors.blue;
    }else if(item.status == K_SUBMIT_SUCCESS) {
      titleBgColor = Colors.green;
    }else if(item.status == K_SUBMIT_FAIL) {
      titleBgColor = Colors.deepOrange;
    }else if(item.status == K_APPEAL_ING) {
      titleBgColor = Colors.orange;
    }else if(item.status == K_APPEAL_SUCCESS) {
      titleBgColor = Colors.teal;
    }else if(item.status == K_APPEAL_FAIL) {
      titleBgColor = Colors.pink;
    }else {
      titleBgColor = Colors.green;

    }

    return new GestureDetector(
      onTap: () {
        onItemClick(item);
      },
      child: Container(
        height: ScreenUtil().setHeight(180),
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(10.0),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            Padding(padding: EdgeInsets.only(left: 15)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "户主:　" + item.username,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(bottom: 10)),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(10.0),
                    child: Container(
                      child: Center(
                        child: Text(
                          item.statusDesc,
                          maxLines: 1,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.white,
                          ),
                        ),
                      ),
                      padding: EdgeInsets.only(top: 1.0),
                      color: titleBgColor,
                      width: 80,
                      height: 30,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
            Text(
              item.updateTime,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 13,
                color: Colors.black54,
              ),
            ),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
      ),
    );
  }
}
